package com.weatherquery.service;

import com.weatherquery.dao.WeatherDAO;
import com.weatherquery.informbean.WeatherData;

import java.awt.print.Book;
import java.util.List;

/**
 * @Author Natural-Pride
 * @Date 2023/12/17 21:28
 * @注释 存放业务功能类。它包含以下方法：
 *      1. listWeather():查询所有天气信息，返回一个包含InformBean对象的列表。
 *      2. modifyWeather(InformBean weather):根据城市ID修改天气信息，如果修改成功则返回true,否则返回false。
 *      3. deleteWeather(String cityId):根据城市ID删除天气信息，如果删除成功则返回true,否则返回false。
 *      4. getWeather(String cityId):根据城市ID查询天气信息，返回一个InformBean对象。
 */
public class WeatherService {
    private WeatherDAO weatherdao=new WeatherDAO();

    // 添加天气业务
    public boolean saveWeather(WeatherData weatherData){
        int i = weatherdao.insertWeather(weatherData);
        return i>0? true: false;
    }

    // 查询所有天气信息
    public List<WeatherData> listWeather(){
        List<WeatherData> weatherList=weatherdao.selectAllWeather();
        return weatherList;
    }

    // 根据城市ID修改天气信息
    public boolean modifyWeather(WeatherData weather){
        int i= weatherdao.updateWeather(weather);
        return i>0?true: false;
    }

    // 根据城市ID删除天气信息
    public boolean deleteWeather(String cityId){
        int i= weatherdao.deleteWeatherById(cityId);
        return i>0?true: false;
    }

    // 根据城市ID查询天气信息
    public WeatherData getWeather(String cityId){
        WeatherData weather=weatherdao.selectWeatherById(cityId);
        return weather;
    }

    // 根据城市名称查询天气信息
    public List<WeatherData> searchWeather(String cityName){
        List<WeatherData> weatherSearchList=weatherdao.searchWeatherByName(cityName);
        return weatherSearchList;
    }

    /*分页展示天气信息
    * pageNum:页码
    * pageSize：每页显示的条数*/
    public List<WeatherData> displayWeatherByPage(int pageNum,int pageSize){
        int start = (pageNum -1) * pageSize;
        int limit = pageSize;
        List<WeatherData> displayWeatherList=weatherdao.displayWeather(start,limit);
        return displayWeatherList;
    }
    /*
    * 查询图书的总页数
    * pageSize:每页显示的条数
    */
    public int getPageSize(int pageSize){
        // 查询总记录数
        long count=weatherdao.selectWeatherCount();
        // 根据页数来获取每页显示的条数
        long pageCount=count% pageSize == 0 ? count / pageSize:  count / pageSize + 1;
        return (int)pageCount;
    }

}
